﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class ClassCreate : System.Web.UI.Page
    {

        private Class _class = new Class();
        private ClassCategory _classCategory = new ClassCategory();

        //Contain involve notice 
        private string _notice;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionInfo).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (!IsPostBack)
                    {
                        loadClassCategory();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }


        /// <summary>
        /// Put Class Category into Drop down list
        /// </summary>
        public void loadClassCategory()
        {
            try
            {
                DataTable dttClassCategory = _classCategory.GetAllClassCategory();
                ddlClassCategory.DataSource = dttClassCategory;
                ddlClassCategory.DataTextField = "ClassCategoryName";
                ddlClassCategory.DataValueField = "ClassCategoryID";
                ddlClassCategory.DataBind();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }


        /// <summary>
        /// Create Class Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btClassCreate_Click(object sender, EventArgs e)
        {
            try
            {
                _notice = "";
                //Check Class Name Condition
                if (txtClassNameCreate.Text == "" || txtClassNameCreate.Text.Length > 50)
                {
                    lblClassNameNotice.Visible = true;
                    lblClassNameNotice.Text = "Tên lớp không được để trống và không quá 50 ký tự";
                    return;
                }
                else
                {
                    lblClassNameNotice.Visible = false;
                    lblClassNameNotice.Text = "";
                }

                //Check Class Position Condition
                if (txtClassPositionCreate.Text.Length > 150)
                {
                    lblClassPositionNotice.Visible = true;
                    lblClassPositionNotice.Text = "Vị trí lớp không quá 150 ký tự";
                    return;
                }
                else
                {
                    lblClassPositionNotice.Visible = false;
                    lblClassPositionNotice.Text = "";
                }


                bool check = _class.ClassCreate(ddlClassCategory.SelectedValue.ToString(), txtClassNameCreate.Text, txtClassPositionCreate.Text);

                //Check inserted or not
                if (check)
                {
                    txtClassNameCreate.Text = "";
                    txtClassPositionCreate.Text = "";
                    _notice = "Đã tạo lớp thành công.";
                }
                else
                {
                    _notice = "Có lỗi hệ thống trong khi tạo lớp.";
                }

                lblNoticeClassCreate.Visible = true;
                lblNoticeClassCreate.Text = _notice;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }


        /// <summary>
        /// Reset Create Class Page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btClassReset_Click(object sender, EventArgs e)
        {
            try
            {
                txtClassNameCreate.Text = "";
                txtClassPositionCreate.Text = "";

                lblClassNameNotice.Visible = false;
                lblClassNameNotice.Text = "";

                lblClassPositionNotice.Visible = false;
                lblClassPositionNotice.Text = "";

                lblNoticeClassCreate.Visible = false;
                lblNoticeClassCreate.Text = "";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }
    }
}